Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Namespace BMLERP
	Friend Class frmINV160
		Inherits System.Windows.Forms.Form
		Dim OldTCTTC As String
		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			'txtTCTTC.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub

		Private Sub cboTCTTC_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTCTTC.ClickEvent
			If eventArgs.area = 2 Then
				WriteToScreen((1))
			End If

		End Sub

		Private Sub chkTCEFA_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTCEFA.CheckStateChanged

			If chkTCEFA.CheckState = 1 Then
				chkTCEFI.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFO.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFR.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFP.CheckState = System.Windows.Forms.CheckState.Unchecked
			End If
		End Sub

		Private Sub chkTCEFI_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTCEFI.CheckStateChanged
			If chkTCEFI.CheckState = 1 Then
				chkTCEFA.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFO.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFR.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFP.CheckState = System.Windows.Forms.CheckState.Unchecked
			End If
		End Sub

		Private Sub chkTCEFO_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTCEFO.CheckStateChanged
			If chkTCEFO.CheckState = 1 Then
				chkTCEFA.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFI.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFR.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFP.CheckState = System.Windows.Forms.CheckState.Unchecked
			End If
		End Sub

		Private Sub chkTCEFP_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTCEFP.CheckStateChanged
			If chkTCEFP.CheckState = 1 Then
				chkTCEFI.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFO.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFR.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFA.CheckState = System.Windows.Forms.CheckState.Unchecked
			End If
		End Sub

		Private Sub chkTCEFR_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTCEFR.CheckStateChanged
			If chkTCEFR.CheckState = 1 Then
				chkTCEFI.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFO.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFA.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFP.CheckState = System.Windows.Forms.CheckState.Unchecked
			End If
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen((0))
			Control((True))
			' Ham Gan tri man hinh
			ButtonStatus()

		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False
			Control((False))
			ButtonStatus()
			txtTCTTC.Text = cboTCTTC.BoundText
			WriteToScreen((1))
		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			ISADDING = False
			ISEDITING = False
			' Ham delete theo Item chon
			Dim SQL As String
			SQL = "DELETE FROM ITC WHERE TCTTC = '" & cboTCTTC.BoundText & "'"

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQL
			cmdExecute.Execute()
			Adodc1.Refresh()
			WriteToScreen((0))
			ButtonStatus()

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click

			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True
			Control((True))

			ButtonStatus()

		End Sub

		Private Sub Control(ByRef GT As Object)

			txtTCTTC.Enabled = GT
			txtTCDLB.Enabled = GT
			txtTCCLB.Enabled = GT
			txtTCDSC.Enabled = GT
			txtTCRLB.Enabled = GT
			txtTCCVL.Enabled = GT
			chkTCEFA.Enabled = GT
			chkTCEFO.Enabled = GT
			chkTCEFR.Enabled = GT
			chkTCCYN.Enabled = GT
			chkTCEFS.Enabled = GT
			chkTCEFP.Enabled = GT
			chkTCEFC.Enabled = GT
			chkTCEFI.Enabled = GT

			'   txtTCTTC.Locked = Not GT
			'    txtTCDLB.Locked = Not GT
			'txtTCCLB.Locked = Not GT
			' txtTCDSC.Locked = Not GT
			'  txtTCRLB.Locked = Not GT
			'   txtTCCVL.Locked = Not GT
			'    chkTCEFA.Locked = Not GT
			'    chkTCEFO.Locked = Not GT
			'    chkTCEFR.Locked = Not GT
			'    chkTCCYN.Locked = Not GT
			'    chkTCEFS.Locked = Not GT
			'    chkTCEFP.Locked = Not GT
			'    chkTCEFC.Locked = Not GT
			'    chkTCEFI.Locked = Not GT

			cboTCTTC.Enabled = Not GT
			cboTCTTC.Locked = GT
		End Sub
		Private Sub WriteToScreen(ByRef tri As Byte)
			Dim TBL As ADODB.Recordset
			If tri = 0 Then	'gan rong
				txtTCTTC.Text = ""
				txtTCDLB.Text = ""
				txtTCCLB.Text = ""
				txtTCDSC.Text = ""
				txtTCRLB.Text = ""
				txtTCCVL.Text = ""
				chkTCEFA.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFO.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFR.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCCYN.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFS.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFP.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFC.CheckState = System.Windows.Forms.CheckState.Unchecked
				chkTCEFI.CheckState = System.Windows.Forms.CheckState.Unchecked
			Else
				TBL = New ADODB.Recordset
				TBL.Open("  Select ITC.*From ITC where  TCTTC ='" & cboTCTTC.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtTCTTC.Text = IIf(IsDBNull(TBL.Fields("TCTTC").Value), "", TBL.Fields("TCTTC").Value)
					txtTCDLB.Text = IIf(IsDBNull(TBL.Fields("tcdlb").Value), "", TBL.Fields("tcdlb").Value)
					txtTCCLB.Text = IIf(IsDBNull(TBL.Fields("tcclb").Value), "", TBL.Fields("tcclb").Value)
					txtTCDSC.Text = IIf(IsDBNull(TBL.Fields("TCDSC").Value), "", TBL.Fields("TCDSC").Value)
					txtTCRLB.Text = IIf(IsDBNull(TBL.Fields("tcrlb").Value), "", TBL.Fields("tcrlb").Value)
					txtTCCVL.Text = IIf(IsDBNull(TBL.Fields("tccvl").Value), "", TBL.Fields("tccvl").Value)
					chkTCEFA.CheckState = IIf(TBL.Fields("TCEFA").Value, 1, 0)
					chkTCEFO.CheckState = IIf(TBL.Fields("TCEFO").Value, 1, 0)
					chkTCEFR.CheckState = IIf(TBL.Fields("TCEFR").Value, 1, 0)
					chkTCCYN.CheckState = IIf(TBL.Fields("TCCYN").Value, 1, 0)
					chkTCEFS.CheckState = IIf(TBL.Fields("TCEFS").Value, 1, 0)
					chkTCEFP.CheckState = IIf(TBL.Fields("TCEFP").Value, 1, 0)
					chkTCEFC.CheckState = IIf(TBL.Fields("TCEFC").Value, 1, 0)
					chkTCEFI.CheckState = IIf(TBL.Fields("TCEFI").Value, 1, 0)
				Else
					txtTCTTC.Text = ""
					txtTCDLB.Text = ""
					txtTCCLB.Text = ""
					txtTCDSC.Text = ""
					txtTCRLB.Text = ""
					txtTCCVL.Text = ""
					chkTCEFA.CheckState = System.Windows.Forms.CheckState.Unchecked
					chkTCEFO.CheckState = System.Windows.Forms.CheckState.Unchecked
					chkTCEFR.CheckState = System.Windows.Forms.CheckState.Unchecked
					chkTCCYN.CheckState = System.Windows.Forms.CheckState.Unchecked
					chkTCEFS.CheckState = System.Windows.Forms.CheckState.Unchecked
					chkTCEFP.CheckState = System.Windows.Forms.CheckState.Unchecked
					chkTCEFC.CheckState = System.Windows.Forms.CheckState.Unchecked
					chkTCEFI.CheckState = System.Windows.Forms.CheckState.Unchecked
				End If
				TBL.Close()
			End If
			OldTCTTC = txtTCTTC.Text
		End Sub
		Private Sub WriteToFile(ByRef tri As Byte)
			Dim SQLInsert As String
			If tri = 0 Then
				SQLInsert = " INSERT INTO ITC " & "(TCTTC,TCDLB,TCCLB,TCDSC,TCRLB,TCCVL,TCEFA,TCEFO,TCEFR,TCCYN, " & "TCEFS,TCEFP,TCEFC,TCEFI)" & "VALUES " & "('" & VB.Left(txtTCTTC.Text, 2) & "', '" & VB.Left(txtTCDLB.Text, 20) & "', " & "'" & VB.Left(txtTCCLB.Text, 20) & "', '" & VB.Left(txtTCDSC.Text, 30) & "', '" & VB.Left(txtTCRLB.Text, 20) & "', '" & VB.Left(txtTCCVL.Text, 20) & "', " & chkTCEFA.CheckState & ", " & chkTCEFO.CheckState & ", " & chkTCEFR.CheckState & ", " & chkTCCYN.CheckState & ", " & chkTCEFS.CheckState & ", " & chkTCEFP.CheckState & ", " & chkTCEFC.CheckState & ", " & chkTCEFI.CheckState & ")"
			Else
				SQLInsert = "UPDATE ITC SET " & " TCTTC ='" & VB.Left(txtTCTTC.Text, 2) & "', TCDLB ='" & VB.Left(txtTCCLB.Text, 20) & "', " & "TCCLB ='" & VB.Left(txtTCCLB.Text, 20) & "', TCDSC ='" & VB.Left(txtTCDSC.Text, 30) & "', " & "TCRLB = '" & VB.Left(txtTCRLB.Text, 20) & "', TCCVL = '" & VB.Left(txtTCCVL.Text, 20) & "', " & "TCEFA =" & chkTCEFA.CheckState & ", TCEFO =" & chkTCEFO.CheckState & ", " & "TCEFR =" & chkTCEFR.CheckState & ", TCCYN = " & chkTCCYN.CheckState & ", " & "TCEFS =" & chkTCEFS.CheckState & ", TCEFP =" & chkTCEFP.CheckState & ", " & "TCEFC =" & chkTCEFC.CheckState & ", TCEFI =" & chkTCEFI.CheckState & " WHERE TCTTC ='" & OldTCTTC & "'"

			End If
			'MsgBox SQLInsert
			DBconn.Execute(SQLInsert)

		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub
		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			' Ham Save tri vao file
			Dim tbl_ChapNhan As ADODB.Recordset
			tbl_ChapNhan = New ADODB.Recordset
			tbl_ChapNhan.Open("  Select IIM.*From IIM ", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
			tbl_ChapNhan.Close()
			If ISADDING = False Then
				WriteToFile((1))
			Else
				WriteToFile((0))
			End If
			Adodc1.Refresh()
			Control((False))
			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()

		End Sub

		Private Sub frmINV160_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			If Not InitSuccess Then
				BIENCHUNG()
			End If

			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "ITC"	'transtion
			Adodc1.Refresh()

			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			VB6_AddADODataBinding()
			Me.cboTCTTC.RowSource = Me.ADOBind_Adodc1.DataSource
			'.Adodc1()
		End Sub

		Private Sub txtTCTTC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTCTTC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldTCTTC, txtTCTTC, "ITC", " TCTTC='" & txtTCTTC.Text & "'") Then
					MsgBox("This code is already exist")
					txtTCTTC.Focus()
				Else
					txtTCDSC.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

	End Class
End Namespace